//
//  LS_BaseNavViewController.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class LS_BaseNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //解决右滑返回手势的问题
        //存在bug 在最顶级的页面 右滑返回手势还会响应
        self.interactivePopGestureRecognizer?.delegate = self

            }
    //重写push方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //判断是否是顶级页面
        var backtitle = "返回"
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1 {
                //设置返回按钮的title = 顶级页面的title
                backtitle = childViewControllers.first?.title ?? "返回"
            }
//            let back = UIBarButtonItem(imageName:"navigationbar_back_withtext", title: backtitle, target: self, action: #selector(popToFrontVC))
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", target: self, action: #selector(popToFrontVC))
            

    viewController.navigationItem.leftBarButtonItem = back
        }
        
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToFrontVC() {
        //pop
        popViewControllerAnimated(true)
    }
    
    //实现手势协议方法
    //一般协议方法返回值 为 bool类型 表示 如果返回 true 就表示对象能够正常工作
    //如果返回false  对象就不能正常工作
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //        if childViewControllers.count > 1 {
        //            //进入子页面
        //            return true
        //        }
        //        return false
        
        return childViewControllers.count > 1
    }



}
